using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using LogFileViewer.Properties;

namespace LogFileViewer
{
    public partial class UserSettings : Form
    {
        public UserSettings()
        {
            InitializeComponent();
            //setTooltips();
        }

        private Settings sets = Settings.Default;

        private void setTooltips()
        {
            // Create the ToolTip and associate with the Form container.
            ToolTip toolTip = new ToolTip();

            // Set up the delays for the ToolTip.
            toolTip.AutoPopDelay = 5000;
            toolTip.InitialDelay = 1000;
            toolTip.ReshowDelay = 500;
            // Force the ToolTip text to be displayed whether or not the form is active.
            toolTip.ShowAlways = true;

            // Set up the ToolTip text for the widgets
            toolTip.SetToolTip(this.chkFilenameHistory, "Hallo");
            toolTip.SetToolTip(this.textBoxFontExample, "Hallo");
        }

        private Font selectedFont;
        private string exampleText = "27.06.2007 16:11:30 [ERROR] org.company.product.example";

        private void btnFontSelect_Click(object sender, EventArgs e)
        {
            fontDialog.Font = sets.ConsoleFont;
            DialogResult result = fontDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                selectedFont = fontDialog.Font;
                this.textBoxFont.Text = selectedFont.Name + ", " + selectedFont.Style.ToString() + ", " + selectedFont.SizeInPoints.ToString() + " pt";
                this.textBoxFontExample.Font = selectedFont;
                this.textBoxFontExample.Text = this.exampleText;
            }
        }

        private void UserSettings_Load(object sender, EventArgs e)
        {
            initSettings();
        }

        private void initSettings()
        {
            // "Remember last open file"
            chkLastOpenFile.Checked = sets.LastOpenFile;

            // "Remember last selected type"
            chkLastTypes.Checked = sets.LastTypes;
            checkedChangedTypes(null, null);

            // "Main window width / height"
            numWidth.Value = sets.MainWindowWidth;
            numHeight.Value = sets.MainWindowHeight;

            // "Main window style"
            comboBoxStyle.SelectedIndex = sets.CurrentWindowStyle;

            // "History"
            chkFilenameHistory.Checked = sets.FilenamesWithoutPath;
            numericHistoryEntries.Value = sets.MaximalHistoryEntries;

            // "Console font"
            selectedFont = sets.ConsoleFont;
            this.textBoxFont.Text = sets.ConsoleFont.Name + ", " + sets.ConsoleFont.Style.ToString() + ", " + sets.ConsoleFont.SizeInPoints.ToString() + " pt";
            this.textBoxFontExample.Font = selectedFont;
            this.textBoxFontExample.Text = this.exampleText;

            // Setting standard start types
            if (sets.StartTypes == null || sets.StartTypes.Count == 0)
            {
                sets.StartTypes = new StringCollection();
                sets.StartTypes.Add(MessageTypes.FATAL.ToString());
                sets.StartTypes.Add(MessageTypes.ERROR.ToString());
            }

            // Prepare types context menu
            for (int i = 0; i < MessageType.Length(); i++)
            {
                string typeName = MessageType.MessageTypeAt(i).ToString();
                ToolStripMenuItem item = new ToolStripMenuItem(typeName);
                if (sets.StartTypes.Contains(typeName))
                {
                    item.Checked = true;
                }
                item.Click += new EventHandler(toolStripMenuItem_Click);

                this.contextMenuTypes.Items.Add(item);
            }
        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            item.Checked = !item.Checked;
        }

        private void btnSizeReset_Click(object sender, EventArgs e)
        {
            numWidth.Value = 522;
            numHeight.Value = 426;
        }

        private void checkedChangedTypes(object sender, EventArgs e)
        {
            btnSelectTypes.Visible = !chkLastTypes.Checked;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // Setting "Remember last open file"
            sets.LastOpenFile = chkLastOpenFile.Checked;

            // Setting "Remember last selected type"
            sets.LastTypes = chkLastTypes.Checked;

            for (int i = 0; i < MessageType.Length(); i++)
            {
                if (i >= this.contextMenuTypes.Items.Count)
                {
                    break;
                }
                ToolStripMenuItem item = (ToolStripMenuItem)this.contextMenuTypes.Items[i];
                if (item != null)
                {
                    if (item.Checked)
                    {
                        sets.StartTypes.Add(item.Text);
                    }
                    else
                    {
                        sets.StartTypes.Remove(item.Text);
                    }
                }
            }

            // "Window width / height"
            sets.MainWindowWidth = (int)numWidth.Value;
            sets.MainWindowHeight = (int)numHeight.Value;

            // Window style
            sets.CurrentWindowStyle = comboBoxStyle.SelectedIndex;

            // "History"
            sets.FilenamesWithoutPath = chkFilenameHistory.Checked;
            sets.MaximalHistoryEntries = (int)numericHistoryEntries.Value;

            // "Console font"
            sets.ConsoleFont = selectedFont;

            // Saving everything
            sets.Save();

            // Closing settings form
            this.Close();
        }

        private void btnSelectTypes_Click(object sender, EventArgs e)
        {
            int posX = this.btnSelectTypes.PointToScreen(new Point(0, 0)).X;
            int posY = this.btnSelectTypes.PointToScreen(new Point(0, 0)).Y + this.btnSelectTypes.Bounds.Height - 1;
            contextMenuTypes.Show(posX, posY);
        }

    }
}